var openDialog;
require(['date','validator'], function () {
	var $saveSet = $("#saveSet"),$formData=$("#formData"),
		lineId,selLineName,lastselect,corpId,nodes,
	    vrsnName="",lineType="",strtusDate="",dateType="",beginTime="",endTime="",beginStnCd="",beginStnSn="",
	    endStnCd="",endStnSn="",minStnDura="",maxStnDura="",vrsnCd="",vrsnCdSel="",strtusDateArr="",corpIds,lineName;

	openDialog = function(selLineId,vrsnCd,corpId,selLineName){
		lineId=selLineId;
		lastselect=vrsnCd;
		corpIds=corpId;
		lineName=selLineName;
		$("#stnDuraVrsnSel").val(lastselect);
		if(lastselect!=""){
			$("#stnDuraForAddOrEdit").val("edit");
		}else{
			$("#stnDuraForAddOrEdit").val("add");
		}
		findInfo();
	};

   function findInfo(){
	   var vrsnCdHtml="";
	   $.ajaxExt({
		   url :"/operationReport/getStnDuraVrsnName.htm",
		   data : {"lineId":lineId,"vrsnCd":lastselect}
	   }).done(function(data){
		   if($("#stnDuraForAddOrEdit").val()=="add"){
			   $("#stnDuraLineType").trigger('change',function(){
				   //清空表单
				   $(':input','#formData')
					   .not(':button, :submit, :reset, :hidden ,:radio')
					   .val('')
					   .removeAttr('checked');
			   });
			   //默认选中通用
			   $("#stnDuraSTNP0001").prop("checked","checked");
		   }else{
			   $(data).each(function(i,item) {
				   vrsnCdHtml+='<option path="'+item.vrsnCd+'" value="'+item.vrsnName+'"'+(i===0?"selected":"")+'>'+item.vrsnName+'</option>'
			   });
			   $("#beginTimeStnDuraSet").val(data[0].beginTime);
			   $("#endTimeStnDuraSet").val(data[0].endTime);
			   $('input[name="stnDuraParameSet"]').prop('checked',false);
			   $("#stnDura"+data[0].dateType).prop("checked",true);
			   $("#stnDuraLineType").val(data[0].lineType);
			   $("#minStnDura").val(data[0].minStnDura);
			   $("#maxStnDura").val(data[0].maxStnDura);
			   $("#stnDuraVrsnName").val(data[0].vrsnName);
			   $("#stnDuraStrtusDate").val(data[0].strtusDate);
			   // $("#stnDuraVrsnSel").html(vrsnCdHtml);
			   // $("#stnDuraVrsnSel").trigger('change');
			   $("#stnDuraLineType").trigger('change',function(){
				   $("#stnDuraBeginStn option").eq(data[0].beginStnSn-1).attr("selected",true);
				   $("#stnDuraEndStn option").eq(data[0].endStnSn-1).attr("selected",true);
			   });
		   }
	   });
   }
    // //根据版本查询线路信息
    // $("#stnDuraVrsnSel").on('change',function(){
		// $.ajaxExt({
		// 	url : "/operationReport/getStnDuraVrsnName.htm",
		// 	data : {"lineId":lineId,"vrsnCd":lastselect}
		// }).done(function(data){
		// 	$("#beginTimeStnDuraSet").val(data[0].beginTime);
		// 	$("#endTimeStnDuraSet").val(data[0].endTime);
		// 	$('input[name="stnDuraParameSet"]').prop('checked',false);
		// 	$("#stnDura"+data[0].dateType).prop("checked",true);
		// 	$("#stnDuraLineType").val(data[0].lineType);
		// 	$("#stnDuraBeginStn option").eq(data[0].beginStnSn).attr("selected",true);
		// 	$("#stnDuraEndStn option").eq(data[0].endStnSn).attr("selected",true);
		// 	$("#minStnDura").val(data[0].minStnDura);
		// 	$("#maxStnDura").val(data[0].maxStnDura);
		// 	$("#stnDuraVrsnName").val(data[0].vrsnName);
		// 	$("#stnDuraStrtusDate").val(data[0].strtusDate);
		// });
    // });

    //根据线路类别填充站点
    $("#stnDuraLineType").on('change',function(e,fn){
		nodes=commonFun.getLeftTreeSelectedNodes();
		if(nodes.length==0){
			$.messager.alert("提示","请选择一条线路查看");
			return;
		}
		var stnDuraLineType = $("#stnDuraLineType").val();
		if(stnDuraLineType=="LIT00003"){
			stnDuraLineType=="LIT00001";
		}
		$.ajaxExt({
			url : "/operationReport/searchStationBehaviorPage.htm",
			data : {"lineId":lineId,"lineType":stnDuraLineType,"stnDuraForAddOrEdit":"add"}
		}).done(function(data){
			var beginStnHtml="";
			$(data).each(function(i,item){
				beginStnHtml+='<option path="'+item.stnCd+'" value="'+item.stnSn+'"'+(i===0?"selected":"")+'>'+item.stnName+'</option>'
			});
			$("#stnDuraBeginStn").html(beginStnHtml);
			$.ajaxExt({
				url : "/operationReport/searchStationBehaviorPage.htm",
				data : {"lineId":lineId,"lineType":stnDuraLineType,"stnDuraForAddOrEdit":"add"}
			}).done(function(data){
				var endStnHtml="";
				$(data).each(function(i,item){
					endStnHtml+='<option path="'+item.stnCd+'" value="'+item.stnSn+'"'+(i===0?"selected":"")+'>'+item.stnName+'</option>'
				});
				$("#stnDuraEndStn").html(endStnHtml);
				if(fn){
					fn();
				}
			});
		});
    });
	//保存按钮
	window.save= function (callBack){
		nodes=commonFun.getLeftTreeSelectedNodes();
		if(nodes.length!=1){
			$.messager.alert("提示","请选择一条线路");
			return;
		}
		// jQuery('#stnDuraSetList').jqGrid('saveRow',lastsel2);
		var flag = $("#stnDuraForAddOrEdit").val();
		vrsnName = $("#stnDuraVrsnName").val();
		lineType = $("#stnDuraLineType").val();
		strtusDate = $("#stnDuraStrtusDate").val();
		beginTime = $("#beginTimeStnDuraSet").val();
		endTime = $("#endTimeStnDuraSet").val();
		beginStnCd =  $('#stnDuraBeginStn').find("option:selected").attr('path');
		beginStnSn = $("#stnDuraBeginStn").val();
		endStnSn = $("#stnDuraEndStn").val();
		endStnCd = $('#stnDuraEndStn').find("option:selected").attr('path');
		dateType = $('input[name="stnDuraParameSet"]:checked').val();
		// if(flag=="add"){
			minStnDura = $("#minStnDura").val();
			maxStnDura = $("#maxStnDura").val();
        if(lineType!='LIT00003'){

            if(beginStnSn>=endStnSn){
                $.messager.alert("提示","开始站点序号不能大于等于结束站点序号");
                return;
            }
        }
		// }else{
		// 	var rowData= $("#stnDuraSetList").getRowData();
		// 	vrsnCdSel =  $('#stnDuraVrsnSel').find("option:selected").attr('path');
		// 	for (var i = 0; i < rowData.length; i ++) {
		// 		strtusDateArr +=rowData[i].strtusDate+",";
		// 		minStnDura +=rowData[i].minStnDura+",";
		// 		maxStnDura +=rowData[i].maxStnDura+",";
		// 		vrsnCd +=rowData[i].vrsnCd+",";
		// 	}
		// }
		var result = $.validator($formData);
		if(result){
			if(flag=="add"){
				//新增
				$.ajaxExt({
					url:"/operationReport/checkStnDura.htm",
					data:{"vrsnName":vrsnName,"lineId":lineId,"beginTime":beginTime,"endTime":endTime,"dateType":dateType,
						"strtusDate":strtusDate,"lineType":lineType, "beginStnCd":beginStnCd,"beginStnSn":beginStnSn,
						"endStnCd":endStnCd,"endStnSn":endStnSn,"minStnDura":$("#minStnDura").val() ,"maxStnDura":$("#maxStnDura").val(),
						"vrsnCd":vrsnCd,"vrsnCdSel":vrsnCdSel,"strtusDateArr":strtusDateArr,"corpId":corpIds}
				}).done(function(){
					$.messager.confirm("提示", "保存成功!", function (r) {
						if (r) {
							callBack();
						}}
					)
				});
			}else{
				$.messager.confirm("提示","是否覆盖  线路:"+lineName+" 的数据？",function(r){
					if(r){
						$.ajaxExt({
							url:"/operationReport/saveStnDura.htm",
							data:{"vrsnName":vrsnName,"lineId":lineId,"beginTime":beginTime,"endTime":endTime,"dateType":dateType,
								"strtusDate":strtusDate,"lineType":lineType, "beginStnCd":beginStnCd,"beginStnSn":beginStnSn,
								"endStnCd":endStnCd,"endStnSn":endStnSn,"minStnDura":$("#minStnDura").val() ,"maxStnDura":$("#maxStnDura").val(),
								"vrsnCd":lastselect,"vrsnCdSel":vrsnCdSel,"strtusDateArr":strtusDateArr,"corpId":corpIds}
						}).done(function(){
							$.messager.confirm("提示", "保存成功!", function (r) {
								if (r) {
									callBack();
								}}
							)
						});
					}
				});
			}
		}
	};

});